//
//  TableHQCDept.m
//  NursingManages
//
//  Created by Zhen Huang on 17/6/2.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "TableHQCDept.h"

@implementation TableHQCDept


-(NSMutableArray *) TransalteToModel:(FMResultSet*) set{
    
    NSMutableArray *array = [NSMutableArray array];
    while ([set next]) {
        HQc_deptModel *model = [[HQc_deptModel alloc]init];
            model.TYPE_ID = [set stringForColumn:@"type_id"];
            model.STANDARD_ID = [set stringForColumn:@"standard_id"];
            model.DEPT_NAME = [set stringForColumn:@"depart_id"];
            model.DEPART_ID = [set stringForColumn:@"dept_name"];
            [array addObject:model];
        
    }
    
    return array;
}


- (NSMutableArray *)GetHQCDept {
    [DBHelper.DB open];
    NSMutableArray *array = [NSMutableArray new];
    array = [self GetHQCDeptWithInputCode:@""];
    [DBHelper.DB close];
    return array;
    
}
- (NSMutableArray *)GetHQCDeptWithInputCode:(NSString *)input_code {
    
    [DBHelper.DB open];
    
    NSString* sql = @"select * from hr_ns_checkdepartment where 1 = 1 %@";
    
    NSString *where = @"";
    
    if (![input_code  isEqual: @""]){
        where = [NSString stringWithFormat:@" and dept_name like '%@%@'",input_code,@"%"];
    }
    sql = [NSString stringWithFormat:sql,where];
    NSString *order = @" %@ order by dept_name asc";
    
    sql = [NSString stringWithFormat:order,sql];
    FMResultSet *set = [DBHelper.DB executeQuery:sql];
    NSMutableArray *array = [self TransalteToModel:set];
    
    
    [DBHelper.DB close];
    return array;
    
    
}


-(HQc_deptModel *) GetModelWithEmpId:(NSString *)EMP_ID{
    [DBHelper.DB open];
    
    NSString* sql = @"select * from hr_ns_checkdepartment where emp_id = '%@'";
    
    sql = [NSString stringWithFormat:sql,EMP_ID];
    
 
    
    FMResultSet *set = [DBHelper.DB executeQuery:sql];
    NSMutableArray *array = [self TransalteToModel:set];
    
    [DBHelper.DB close];
    
    
    
    if (array.count != 0) {
        return array[0];
    }
    
    
    return [[HQc_deptModel alloc]init];
}

-(NSMutableArray *) GetHQCDeptArray:(NSString *)standard_id input_Code:(NSString *)input_Code
{
    [DBHelper.DB open];
    
    NSString* sql = @"select * from hr_ns_checkdepartment where standard_id = '%@' ";
    
    sql = [NSString stringWithFormat:sql,standard_id];
    
    if (![input_Code  isEqual: @""]){
        sql = [NSString stringWithFormat:@" %@ and dept_name like '%@%@'",sql,input_Code,@"%"];
    }
    
    NSString *order = @" %@ order by dept_name asc";
    
    sql = [NSString stringWithFormat:order,sql];

    FMResultSet *set = [DBHelper.DB executeQuery:sql];
    NSMutableArray *array = [self TransalteToModel:set];
    
    [DBHelper.DB close];
    return array;

}


@end
